<?php
class JM_DebugUtils
{
    private static $_instance;
    private static $_vars;
    
    
    protected function __construct()
    {    }


    public static function &get_instance()
    {
        if( !self::$_instance  )
        {
            self::$_instance = new JM_DebugUtils();
        }
        return self::$_instance;
    }


    /**
    * @param string The key to get.
    * @return mixed The stored data, or null
    */
    public static function get_Debug_Data($key)
    {
        if( is_array( self::$_vars ) && isset(self::$_vars[$key]) )
        {
            return self::$_vars[$key];
        }
    }


  /**
   * Set data for later use.
   * This data is not stored in the session, so it only exists for one request.
   * @param string The name of this data.
   * @param mixed  The data to store.
   */
  public static function set_Debug_Data($key,$value)
  {
    if( $key == '' ) return;
    if( !is_array(self::$_vars) )
      {
          self::$_vars = array();
      }
    self::$_vars[$key] = $value;
  }
  
  /**
  * Convenience function to return the var_dump
  * 
  * @param mixed $var
  * @param mixed $formated - if true -> '<em>' . var_dump($var) . '<\em>'
  * @return string
  */
  public static function  varDumpToString ($var, $formated = FALSE)

    {
        ob_start();
        var_dump($var);
        
        if ($formated)
        {
            $result = '<em>' . ob_get_clean() . '<\em>';    
        }
        else $result = ob_get_clean();
        
        return $result;
    }
    
    /**
    * Convenience function to return a em formated print_r
    * 
    * @param mixed $var
    */
    public static function print_r_frm($var)
    {
        return '<em>' . print_r($var, true) . '<\em>';
    }
    
    public static function varDumpToFile ($var, $fileName =  'log.txt', $formated = FALSE)
    {
        $handle = fopen($fileName, 'a') or die('Cannot open file:  ' . $fileName);
        $date = new DateTime();
        
        $data = $date->format('U = Y-m-d H:i:s') . "\n\n";
        fwrite($handle, $data);
        $data = self::varDumpToString($var, $formated);
        fwrite($handle, $data);
        $new_data = "\n\n";
        fwrite($handle, $new_data);
        fclose($handle);
    }    
}
?>
